package com.zzld.modules.maintenance.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.abel533.entity.Example;
import com.github.pagehelper.PageInfo;
import com.zzld.core.utils.MessageUtils;
import com.zzld.modules.common.dto.CommonJSON;
import com.zzld.modules.maintenance.entity.Matnexpense;
import com.zzld.modules.maintenance.service.IMatnexpenseService;
import com.zzld.modules.maintenance.service.IMatnexpensepathService;

@Controller
@RequestMapping("/maintenance/expense")
public class MtanExpenseController {

	@Autowired
	private IMatnexpenseService expenseService;

	@Autowired
	private IMatnexpensepathService expensepathService;

	private String listurl = "/modules/maintenance/expense/expenseList";
	private String listcru = "/modules/maintenance/expense/expenseCru";
	private String listdetail = "/modules/maintenance/expense/expenseDetial";

	@RequestMapping(method = RequestMethod.GET)
	public String li() {
		return listurl;
	}

	@RequestMapping("/Cru")
	public String cru() {
		return listcru;
	}

	@RequestMapping("/detail")
	public String detail() {
		return listdetail;
	}

	@RequestMapping("/list")
	@ResponseBody
	public CommonJSON page(HttpServletRequest request) {
		CommonJSON json = new CommonJSON();
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("phonenum", request.getParameter("phonenum"));
		map.put("storename", request.getParameter("storename"));
		map.put("exstartime", request.getParameter("exstartime"));
		map.put("exendtime", request.getParameter("exendtime"));

		Integer PageNum = Integer.parseInt(request.getParameter("page"));
		Integer PageSize = Integer.parseInt(request.getParameter("rows"));

		PageInfo pageinfo = expensepathService.list(map, PageNum, PageSize);
		json.setRows(pageinfo.getList());
		json.setTotal(pageinfo.getTotal());
		return json;
	}

	@RequestMapping("/detail/update/{id}")
	@ResponseBody
	public CommonJSON update(HttpServletRequest request,
			@RequestParam String id) {
		CommonJSON json = new CommonJSON();
		if (StringUtils.isNotBlank(id)) {
			Matnexpense entity = new Matnexpense();
			entity.setId(id);
			entity.setIspro(1);
			try {
				if (expenseService.updateByPrimaryKeySelective(entity) > 0) {
					json.setMessage(MessageUtils.EDIT_SUCCESS);
					json.setSuccess(true);
				}else{
					json.setMessage(MessageUtils.EDIT_WARING);
					json.setSuccess(false);
				}
			} catch (Exception e) {
                   json.setMessage(MessageUtils.EDIT_WARING);
                   json.setSuccess(false);
			}
		}
		return json;
	}

	@RequestMapping("/detail/list")
	@ResponseBody
	public CommonJSON detiallist(HttpServletRequest request) throws Exception {
		String id = request.getParameter("id");

		CommonJSON json = new CommonJSON();

		if (StringUtils.isNotBlank(id)) {

			List<Matnexpense> list = expenseService.selectbydetail(id);
			if (list.size() > 0) {
				json.setRows(list);
			}
		}

		return json;

	}

}
